# -*- coding: utf-8 -*-

import peewee
import os_package
from datetime import datetime

# 实例化os_package.Os_Option类
os_opt = os_package.Os_Option()
# 调用os_opt.make_file_folder方法，创建名为database的文件夹
os_opt.make_file_folder('database')
# 实例化peewee.SqliteDatabase类，连接名为database的文件夹下的当前文件名.db数据库
db = peewee.SqliteDatabase(f'database/{os_opt.get_current_file_name()[0]}.db')


class BaseModel(peewee.Model):
    class Meta:
        database = db


class Jinniu_Teacher_Info(BaseModel):
    """教师信息表模型"""

    class Meta:
        table_name = 'jinniu_teacher_info'

    # 教师ID
    id:int= peewee.AutoField(primary_key=True, unique=True,null=False)
    # 教师姓名
    teacher_name:str = peewee.CharField(max_length=255,null=False)
    # 教师账号
    teacher_account:str = peewee.CharField(max_length=255, unique=True,null=False)
    # 教师密码
    teacher_password:str = peewee.CharField(max_length=255,null=False)
    # 教师账号状态
    teacher_active:bool = peewee.BooleanField(default=True)
    # 教师账号当前登陆状态
    teacher_login_status:bool = peewee.BooleanField(default=False)
    # 注册时间
    registration_time:datetime = peewee.DateTimeField(default=datetime.now,null=False)
    # 上一次登陆时间
    last_login_time:datetime = peewee.DateTimeField(default=datetime.now,null=False)
    # 备注
    remarks:str = peewee.TextField(null=True)

class Jinniu_School_Info(BaseModel):
    """学校信息表模型"""

    class Meta:
        table_name = 'jinniu_school_info'

    # 学校ID
    id:int= peewee.AutoField(primary_key=True, unique=True,null=False)
    # 省份
    province:str = peewee.CharField(max_length=255,null=False)
    # 市区
    urban_area:str = peewee.CharField(max_length=255,null=False)
    # 区县
    district_and_county:str = peewee.CharField(max_length=255,null=False)
    # 学校名字
    school_name:str = peewee.CharField(max_length=255,null=False,unique=True)
    # 学校地址
    school_address:str = peewee.CharField(max_length=255,null=False)
    # 学校状态
    school_active:bool = peewee.BooleanField(default=True,null=False)
    # 合作年月
    cooperation_period:str = peewee.CharField(max_length=255,null=False)
    # 支教负责人
    person_teacher:str = peewee.ForeignKeyField(Jinniu_Teacher_Info, column_name='person_teacher', backref='schools',null=False)
    # 备注
    remarks:str = peewee.TextField(null=True)

    @classmethod
    # 查询教师账号绑定的学校
    def get_school_by_teacher_account(cls, teacher_account):
        return cls.select().where(cls.person_teacher == teacher_account)

class Jinniu_Student_Info(BaseModel):
    """学生信息表模型"""

    class Meta:
        table_name = 'jinniu_student_info'

    # 学生ID
    id:int= peewee.AutoField(primary_key=True, unique=True,null=False)
    # 学生姓名
    student_name: str = peewee.CharField(max_length=255,null=False)
    # 班级名
    class_name: str = peewee.CharField(max_length=255,null=False)
    # 学生隶属学校
    student_school: str = peewee.ForeignKeyField(Jinniu_School_Info, column_name='student_school', backref='student_schools',null=False)
    # 学生年级
    student_grade: str = peewee.CharField(max_length=255,null=False)
    # 学生班级
    student_class: int = peewee.IntegerField(null=False)
    # 学生类型 == 普教班、竞赛班
    student_type: str = peewee.CharField(max_length=255,null=False)
    # 学生状态 == 是否退课
    student_status: bool = peewee.BooleanField(default=True,null=False)
    # 授课教师
    instructor_teacher:str = peewee.ForeignKeyField(Jinniu_Teacher_Info, column_name='instructor_teacher', backref='students',null=False)
    # 备注
    remarks: str = peewee.TextField(null=True)

    @classmethod
    # 查询教师账号绑定的学生
    def get_student_by_teacher_name(cls, teacher_name):
        return cls.select().where(cls.instructor_teacher == teacher_name)

    @classmethod
    # 查询学校绑定的学生
    def get_student_by_school_name(cls, school_name):
        return cls.select().where(cls.student_school == school_name)

class Jinniu_Card_Info(BaseModel):
    """卡片信息表模型"""

    class Meta:
        table_name = 'jinniu_card_info'

    # 卡片ID
    id:int= peewee.AutoField(primary_key=True, unique=True,null=False)
    # 卡片账号
    card_account:str = peewee.CharField(max_length=255, null=False,unique=True)
    # 卡片类型
    card_type: str = peewee.CharField(max_length=255, null=False)
    # 注册学生id
    register_student_id = peewee.ForeignKeyField(Jinniu_Teacher_Info, column_name='register_student_id', backref='student_id_card',null=False)
    # 是否过期
    card_is_expired:bool = peewee.BooleanField(default=False,null=False)
    # 备注
    remarks:str = peewee.TextField(null=True)

    @classmethod
    # 查询卡片账号绑定的学生id
    def get_card_account_by_student_id(cls, student_id):
        return cls.select().where(cls.card_account == student_id)
